package ch.fhnw.edu.rental.daos.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import ch.fhnw.edu.rental.daos.PriceCategoryDAO;
import ch.fhnw.edu.rental.model.Movie;
import ch.fhnw.edu.rental.model.PriceCategory;
import ch.fhnw.edu.rental.model.PriceCategoryChildren;
import ch.fhnw.edu.rental.model.PriceCategoryNewRelease;
import ch.fhnw.edu.rental.model.PriceCategoryRegular;

public class JdbcPriceCategoryDAO extends SimpleJdbcDaoSupport implements PriceCategoryDAO {

	@Override
	public PriceCategory getById(Long id) {
		SimpleJdbcTemplate template = getSimpleJdbcTemplate();
		Map<String, Object> res = template.queryForMap("select * from PRICECATEGORIES where PRICECATEGORY_ID = ?", new Object[]{id});
		String type = (String) res.get("PRICECATEGORY_TYPE");
		PriceCategory pc;
		if ("Children".equals(type)){
			pc = new PriceCategoryChildren();
			pc.setId(id);
		} else if ("Regular".equals(type)) {
			pc = new PriceCategoryRegular();
			pc.setId(id);
		} else if ("NewRelease".equals(type)) {
			pc = new PriceCategoryNewRelease();
			pc.setId(id);
		} else {
			pc = null;
		}
		return pc;
	}

	@Override
	public List<PriceCategory> getAll() {
		SimpleJdbcTemplate template = getSimpleJdbcTemplate();
		return template.query("select * from PRICECATEGORIES",
							  new RowMapper<PriceCategory>(){
							  	public PriceCategory mapRow(ResultSet rs, int row) throws SQLException {
							  		String type = rs.getString("PRICECATEGORY_TYPE");
							  		Long id = rs.getLong("PRICECATEGORY_ID");
							  		PriceCategory pc;
									if ("Children".equals(type)){
										pc = new PriceCategoryChildren();
										pc.setId(id);
									} else if ("Regular".equals(type)) {
										pc = new PriceCategoryRegular();
										pc.setId(id);
									} else if ("NewRelease".equals(type)) {
										pc = new PriceCategoryNewRelease();
										pc.setId(id);
									} else {
										pc = null;
									}
									return pc;
							  	}
							  }
							  );
	}

	@Override
	public void saveOrUpdate(PriceCategory priceCategory) {
		// TODO Auto-generated method stub
	}

	@Override
	public void delete(PriceCategory priceCategory) {
		// TODO Auto-generated method stub
	}
}
